//
//  LoginViewController.m
//  DAlbum
//
//  Created by David Qiu on 15/9/2.
//  Copyright (c) 2015年 David Qiu. All rights reserved.
//

#import "LoginViewController.h"


@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *textFieldUsername;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPassword;
@property (weak, nonatomic) IBOutlet UIButton* buttonLogin;

@property (strong, atomic) NSString* username;
@property (strong, atomic) NSString* password;

@end


@implementation LoginViewController

#pragma mark - View Control

- (void) viewDidLoad {
    [super viewDidLoad];
    
    // Initialize: Account
    self.username = @"";
    self.password = @"";
    
    // Initialize: Account text fields
    self.textFieldUsername.delegate = self;
    self.textFieldPassword.delegate = self;
}

- (void) didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // Focus on username text field
    [self.textFieldUsername becomeFirstResponder];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    // Clear the focus on username or password text fields
    [self.textFieldUsername resignFirstResponder];
    [self.textFieldPassword resignFirstResponder];
}


#pragma mark - Text Field Delegates

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    // Case: textFieldUsername
    if (textField == self.textFieldUsername) {
        [self.textFieldPassword becomeFirstResponder];
    }
    
    // Case: textFieldPassword
    if (textField == self.textFieldPassword) {
        [self doLogin];
    }
    
    return YES;
}


#pragma mark - Login

- (BOOL) doLogin {
    // Fetch login information
    NSString* username = self.textFieldUsername.text;
    NSString* password = self.textFieldPassword.text;
    
    // Inform login
    NSLog(@"[ INFO ] Login. (username: \"%@\", password: \"%@\")", username, password);
    
    // Check username and password
    if ([username isEqualToString:self.username] && [password isEqualToString:self.password]) {
        // Inform login succeeded
        NSLog(@"[ INFO ] Login succeeded. (username: \"%@\")", self.textFieldUsername.text);
        
        // Trigger login succeeded segue
        [self performSegueWithIdentifier:@"segueLoginSucceeded" sender:self];
        
        // Return success
        return YES;
    } else {
        // Inform login failed
        NSLog(@"[ INFO ] Login failed. (username: \"%@\")", self.textFieldUsername.text);
        
        // Clear the text fields
        self.textFieldUsername.text = @"";
        self.textFieldPassword.text = @"";
        [self.textFieldUsername becomeFirstResponder];
        
        // Return failure
        return NO;
    }
}


#pragma mark - Login UI View Events

- (IBAction) buttonLoginClicked:(id)sender {
    // Login
    [self doLogin];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
